package net.htjs.juc.parkunpark;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 与Object的wait和notify相比
 * 1.wait,notify和notifyAll必须配合object monitor一起使用，而park,unpark不用
 * 2.park & unpark是以线程为单位【阻塞】和【唤醒线程】，而notify只能随机唤醒一个等待线程
 * notifyAll是唤醒所有等待线程，就不那么精确
 * 3.park & unpark可以先unpark,而wait & notify 不能先notify
 */
@Slf4j
public class ParkUnparkDemo {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("running...");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.debug("park...");
            LockSupport.park();
            log.debug("resume...");
        }, "t1");
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        log.debug("unpark...");
        LockSupport.unpark(t1);
    }
}
